package top.jle.wx.proxy.api.oauth;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.jle.wx.proxy.annotation.AccessToken;
import top.jle.wx.proxy.api.ResponseBack;
import top.jle.wx.proxy.constant.SNSScope;
import top.jle.wx.proxy.domain.fans.FansInfo;
import top.jle.wx.proxy.facade.BasicFacade;
import top.jle.wx.proxy.util.GlobalThreadLocalUtil;

import java.util.Map;

@Controller
@RequestMapping("/oauth")
@Tag(name = "OAuth API")
public class OAuthAPI {

    @Autowired
    private BasicFacade basicFacade;

    @Operation(summary = "微信授权Code获取用户信息,根据Scope不同获取的信息量会不同")
    @GetMapping("/sns/{code}")
    @ResponseBody
    public ResponseBack<FansInfo> snsByCode(@Parameter(description="微信登录授权码") @PathVariable String code){
        return ResponseBack.success(basicFacade.loadSnsUserInfo(code));
    }
    @Operation(summary="微信小程序Code2Session")
    @GetMapping("/sns/code2Session/{jsCode}")
    @ResponseBody
    public ResponseBack<SNSScope.Code2SessionResponse> code2Session(@Parameter @PathVariable String jsCode){
        return ResponseBack.success(basicFacade.code2Session(jsCode));
    }

    @Operation(summary="转换URL（"+SNSScope.SNSAPI_USERINFO+"）")
    @PostMapping("/sns/convertUrl/oauth")
    @ResponseBody
    public ResponseBack<String> convertUrlToOauthUrl(String url,String state) {
        return ResponseBack.success(basicFacade.convertUrlToOauthUrl(url,state));
    }

    @Operation(summary="转换URL（"+SNSScope.SNSAPI_BASE+"）仅能获取OpenId")
    @PostMapping("/sns/convertUrl/base")
    @ResponseBody
    public ResponseBack<String> convertUrlToBaseUrl(String url,String state) {
        return ResponseBack.success(basicFacade.convertUrlToSNSBASEUrl(url,state));
    }

    @Operation(summary="获取JSSDK配置对象")
    @GetMapping("/sns/getJSSDKConfig")
    @ResponseBody
    @AccessToken
    public ResponseBack<SNSScope.JSSDKConfig> getJSSDKConfig(@Parameter(description="使用SDK所在页面的URL") @RequestParam String url){
        return ResponseBack.success(basicFacade.getJSSDKConf(url));
    }

    @Operation(summary="微信网页授权代理页面（snsapi_userinfo)")
    @GetMapping("/OauthUserInfo")
    public String sendOauthUserInfoRequestForWOA(Map<String,Object> data,@Parameter(description="回调URL") @RequestParam String url, @Parameter(description="场景参数") @RequestParam(required = false) String state){
        data.put("appId", GlobalThreadLocalUtil.getAppId());
        data.put("url",url);
        data.put("scope", SNSScope.SNSAPI_USERINFO);
        data.put("state",state);
        return "getWxCode";
    }
    @Operation(summary="微信网页授权代理页面（snsapi_base)")
    @GetMapping("/OauthSNSBase")
    public String sendOauthSNSBaseRequestForWOA(Map<String,Object> data,@Parameter(description="回调URL") @RequestParam String url,@Parameter(description="场景参数") @RequestParam(required = false) String state){
        data.put("appId", GlobalThreadLocalUtil.getAppId());
        data.put("url",url);
        data.put("scope", SNSScope.SNSAPI_BASE);
        data.put("state",state);
        return "getWxCode";
    }

}
